package cn.iocoder.yudao.module.system.service.alluser;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.CommonConstant;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO;
import cn.iocoder.yudao.module.system.service.member.MemberService;
import cn.iocoder.yudao.module.system.service.permission.PermissionService;
import cn.iocoder.yudao.module.system.service.permission.RoleService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.dal.dataobject.alluser.AllUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.alluser.AllUserMapper;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static java.util.Arrays.asList;
import static java.util.Collections.singleton;

/**
 * Auth Service 实现类
 *
 * @author 芋道源码
 */
@Service
public class AllUserServiceImpl implements AllUserService {
    @Resource
    private AllUserMapper allUserMapper;
    @Resource
    private AdminUserService userService;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private MemberService memberService;
//    @Resource
//    private RoleService roleService;
//    @Resource
//    private PermissionService permissionService;
    @Override
    public PageResult<AllUserDO> findAllUserPage(Integer pageNum, Integer pageSize, String enterprise,Integer type,String username) {
        IPage<AllUserDO> allUserPage = allUserMapper.findAllUserPage(new Page<>(pageNum, pageSize), enterprise,type,username);
        PageResult<AllUserDO> pageResult = new PageResult<>();
        pageResult.setList(allUserPage.getRecords());
        pageResult.setTotal(allUserPage.getTotal());
        return  pageResult;
    }

    @Override
    public AllUserDO findAllUserDetail(String id) {
        return allUserMapper.findAllUserDetail(id);
    }

    @Override
    public void createAllUser(AllUserDO allUserDO) {
        String type = allUserDO.getType();
        if(ObjectUtil.isNull(type)||!asList("1","2").contains(type)){
            throw exception(new ErrorCode(500,"角色类型不能为空且只能是客户或普通管理员"));
        }

        String nickname = allUserDO.getEnterprise();
        String username = allUserDO.getUsername();
        String mobile = allUserDO.getMobile();
        String plainPassword = allUserDO.getPlainPassword();
        if(ObjectUtil.equal(type,"1")){
            memberService.createUser(username,nickname,mobile,plainPassword);
        }else{
            UserSaveReqVO reqVO = new UserSaveReqVO();
            reqVO.setMobile(mobile);
            reqVO.setPlainPassword(plainPassword);
            reqVO.setUsername(username);
            reqVO.setNickname(nickname);
            userService.createAllUser(reqVO);

        }

    }

    @Override
    public void deleteAllUser(String id) {
        if(ObjectUtil.isNull(id)){
            throw exception(new ErrorCode(500,"id不能为空"));
        }
        String[] split = id.split("-");
        if(split.length!=2||!asList("1","2").contains(split[0])){
            throw exception(new ErrorCode(500,"id错误"));
        }
        String type = split[0];
        Long dataId = Long.parseLong(split[1]);
        if(ObjectUtil.equal(type,"1")){
            memberService.deleteUser(dataId);
        }else{
            userService.deleteAllUser(dataId);
        }
    }

    @Override
    public void updateAllUser(AllUserDO allUserDO) {
        String id = allUserDO.getId();
        if(ObjectUtil.isNull(id)){
            throw exception(new ErrorCode(500,"id不能为空"));
        }
        String[] split = id.split("-");
        if(split.length!=2||!asList("1","2").contains(split[0])){
            throw exception(new ErrorCode(500,"id错误"));
        }
        String type = split[0];
        Long dataId = Long.parseLong(split[1]);
        String nickname = "";
        String enterprise = allUserDO.getEnterprise();
        if(StrUtil.isBlank(nickname)){
            nickname=enterprise;
        }
        String username = allUserDO.getUsername();
        String mobile = allUserDO.getMobile();
        String plainPassword = allUserDO.getPlainPassword();
        if(ObjectUtil.equal(type,"1")){
            memberService.updateUser(dataId,username,nickname,mobile,plainPassword);
        }else{
            UserSaveReqVO reqVO = new UserSaveReqVO();
            reqVO.setId(dataId);
            reqVO.setMobile(mobile);
            reqVO.setPlainPassword(plainPassword);
            reqVO.setPassword(passwordEncoder.encode(plainPassword));
            reqVO.setUsername(username);
            reqVO.setNickname(nickname);
            userService.updateAllUser(reqVO);
        }
    }
}
